package net.shopxx.dao.impl;

import java.util.List;

import net.shopxx.bean.Pager;
import net.shopxx.bean.Pager.OrderType;
import net.shopxx.dao.DeptDao;
import net.shopxx.entity.Dept;

import org.hibernate.criterion.DetachedCriteria;
import org.springframework.stereotype.Repository;

/**
 * Dao实现类 - 部门
 */

@Repository
public class DeptDaoImpl extends BaseDaoImpl<Dept, String> implements DeptDao {

	@SuppressWarnings("unchecked")
	public List<Dept> getRootDeptList() {
		String hql = "from Dept dept where dept.parent is null order by dept.orderList asc";
		return getSession().createQuery(hql).list();
	}
	
	@SuppressWarnings("unchecked")
	public List<Dept> getParentDeptList(Dept dept) {
		String hql = "from Dept dept where dept != ? and dept.id in(:ids) order by dept.orderList asc";
		String[] ids = dept.getPath().split(Dept.PATH_SEPARATOR);
		return getSession().createQuery(hql).setParameter(0, dept).setParameterList("ids", ids).list();
	}
	
	@SuppressWarnings("unchecked")
	public List<Dept> getChildrenDeptList(Dept dept) {
		String hql = "from Dept dept where dept != ? and dept.path like ? order by dept.orderList asc";
		return getSession().createQuery(hql).setParameter(0, dept).setParameter(1, dept.getPath() + "%").list();
	}
	
	// 重写方法，保存时计算path值
	@Override
	public String save(Dept dept) {
		String id = super.save(dept);
		Dept parent = dept.getParent();
		if (parent != null) {
			String parentPath = parent.getPath();
			dept.setPath(parentPath + Dept.PATH_SEPARATOR + id);
		} else {
			dept.setPath(id);
		}
		super.update(dept);
		return id;
	}
	
	// 重写方法，更新时计算path值
	@Override
	public void update(Dept dept) {
		Dept parent = dept.getParent();
		if (parent != null) {
			String parentPath = parent.getPath();
			dept.setPath(parentPath + Dept.PATH_SEPARATOR + dept.getId());
		} else {
			dept.setPath(dept.getId());
		}
		super.update(dept);
	}
	
	// 根据orderList排序
	@SuppressWarnings("unchecked")
	@Override
	public List<Dept> getAll() {
		String hql = "from Dept dept order by dept.orderList asc dept.createDate desc";
		return getSession().createQuery(hql).list();
	}

	// 根据orderList排序
	@Override
	@SuppressWarnings("unchecked")
	public List<Dept> getList(String propertyName, Object value) {
		String hql = "from Dept dept where dept." + propertyName + "=? order by dept.orderList asc dept.createDate desc";
		return getSession().createQuery(hql).setParameter(0, value).list();
	}
	
	// 根据orderList排序
	@Override
	public Pager findByPager(Pager pager, DetachedCriteria detachedCriteria) {
		if (pager == null) {
			pager = new Pager();
			pager.setOrderBy("orderList");
			pager.setOrderType(OrderType.asc);
		}
		return super.findByPager(pager, detachedCriteria);
	}

	// 根据orderList排序
	@Override
	public Pager findByPager(Pager pager) {
		DetachedCriteria detachedCriteria = DetachedCriteria.forClass(Dept.class);
		return this.findByPager(pager, detachedCriteria);
	}

}